package com.base.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class StringHandler {

    /**
     * 移除指定的字符
     *
     * @param sb         StringBuffer对象
     * @param remove_str 要移除的对象
     *                   使用示例:
     *                   StringBuffer sb = new StringBuffer("ab,cd,ef,");
     *                   String str = StringHandler.RemoveStr(sb,",");
     *                   str 结果就是 "ab,cd,ef"
     * @return
     */
    public static String RemoveStr(StringBuffer sb, String remove_str) {
        String str = "";
        if (null != sb && sb.length() > 0) {
            if (remove_str != null && sb.indexOf(remove_str) != -1) {
                str = sb.deleteCharAt(sb.lastIndexOf(remove_str)).toString();
            } else {
                str = sb.toString();
            }
        }
        return str;
    }

    /**
     * 从源数组中移除指定的多个数据
     *
     * @param sourceArr String[] 对象
     * @param removeArr String[] 要移除的对象
     *                  使用示例:
     *                  String sourceArr = new String[]{"ab","cd","ef,"};
     *                  String removeArr = new String[]{"ab"};
     *                  str 结果就是 "cd","ef"
     * @return
     */
    public static String[] Removes(String[] sourceArr, String[] removeArr) {
        List<String> list = Arrays.asList(sourceArr);
        if (null == list || list.size() == 0) return null;
        if (null == removeArr || removeArr.length == 0) return sourceArr;
        List<String> newList = new ArrayList<String>(list);
//		List<Integer> delIndexList = new ArrayList<Integer>();
        for (int i = 0, count = list.size(); i < count; i++) {
//			boolean isDel = false;
            String sourceStr = list.get(i);
            for (String removeStr : removeArr) {
                if (sourceStr.equals(removeStr)) {
                    newList.remove(removeStr);
//					isDel = true;
                    break;
                }
            }
//			if(isDel) delIndexList.add(i);
        }
//		if(null == delIndexList || delIndexList.size() == 0) return sourceArr;
//		for(int i=0,count = delIndexList.size(); i<count; i++){
//			int delIndex = delIndexList.get(i);
//			newList.remove(delIndex);
//		}
        String[] newArr = new String[1];
        newArr = newList.toArray(newArr);
        return newArr;
    }


    /**
     * 移除最后一个逗号
     *
     * @param sb StringBuffer对象
     *           使用示例:
     *           StringBuffer sb = new StringBuffer("ab,cd,ef,");
     *           String str = StringHandler.RemoveStr(sb);
     *           str 结果就是 "ab,cd,ef"
     * @return
     */
    public static String RemoveStr(StringBuffer sb) {
        return RemoveStr(sb, ",");
    }

    /**
     * 移除最后一个逗号
     *
     * @param sb StringBuilder对象
     *           使用示例:
     *           StringBuffer sb = new StringBuffer("ab,cd,ef,");
     *           String str = StringHandler.RemoveStr(sb);
     *           str 结果就是 "ab,cd,ef"
     * @return
     */
    public static String RemoveStr(StringBuilder sb) {
        return RemoveStr(new StringBuffer(sb));
    }

    /**
     * 移除指定的字符
     *
     * @param str        StringBuffer对象
     * @param remove_str 要移除的对象
     *                   使用示例:
     *                   String strids ="ab,cd,ef,";
     *                   String str = StringHandler.RemoveStr(strids,",");
     *                   str 结果就是 "ab,cd,ef"
     * @return
     */
    public static String RemoveStr(String str, String remove_str) {
        StringBuffer sb = new StringBuffer(str);
        return RemoveStr(sb, remove_str);
    }

    /**
     * 将字符串数组用“，”连接成字符串
     *
     * @param list List 对象
     * @return 连接后的字符串
     */
    public static String join(List<?> list) {
        if (null == list || list.size() == 0) return "";
        Object[] arr = new Object[1];
        arr = list.toArray(arr);
        StringBuffer sb = new StringBuffer();
        for (Object str : arr) {
            if (null == str || str.equals("")) continue;
            sb.append(str + ",");
        }
        return RemoveStr(sb);
    }


    /**
     * 将字符串数组用“，”连接成字符串
     *
     * @param arr Object 数组
     * @return 连接后的字符串
     */
    public static String join(Object[] arr) {
        StringBuffer sb = new StringBuffer();
        for (Object str : arr) {
            if (null == str || str.equals("")) continue;
            sb.append(str + ",");
        }
        return RemoveStr(sb);
    }

    /**
     * 分割字符串为字符串数组
     * @param strs
     * @param op
     * @return
     */
    public static String[] splitStr(String strs,String op)
    {
        if(isValidStr(strs) && isValidStr(op))
        {
            return strs.split(op);
        }
        return null;
    }

    /**
     * 验证字符串是否为 null 或 ""
     * 不为 null 或 "" 返回 True 否则返回 false
     * @param str
     * @return
     */
    public static boolean isValidStr(String str)
    {
        return (null != str && !"".equals(str) && !"null".equals(str)) ? true : false;
    }

    /**
     * 验证字符串是否为 null 或 ""
     * 不为 null 或 "" 返回 True 否则返回 false
     * @param obj
     * @return
     */
    public static boolean isValidObj(Object obj)
    {
        return (null != obj && !"".equals(obj.toString())) ? true : false;
    }

    /**
     * 验证整型对象是否为空
     * @param <T>
     * @param obj
     * @return 如果为空就返回 false 否则为 true
     */
    public static <T> boolean isValidIntegerNull(Integer obj){
        if(null != obj && 0 !=obj) return true;
        return false;
    }
    /**
     * 将字符串转换为整型，如果字符串为 空 或 “” 就为0
     * @param val
     * @return
     */
    public static Integer convertInt(String val){
        return (null == val || "".equals(val)) ? 0 : Integer.parseInt(val);
    }

    /**
     * 获取URL参数
     * @Author Mr.Shaw
     * @Date 2020/8/9 21:35
     */
    public static String getUrlParam(String url, String name) {
        String result = "";
        int index = url.indexOf("?");
        String temp = url.substring(index + 1);
        String[] keyValue = temp.split("&");
        for (String str : keyValue) {
            if (str.contains(name)) {
                result = str.replace(name + "=", "");
                break;
            }
        }
        return result;
    }
}
